package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParamLogin;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonAnyFormatVisitor;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.plugin.PluginURLJarFileCallBack;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.PastOrPresent;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserInfoController  {
    /**
     *  配置文件中的手机验证码
     */
//    @Value("${sms.url}")
//    private String smsUrl;
//    @Value("${sms.sign}")
//    private String smsSign;
//    @Value("${sms.appkey}")
//    private String smsAppkey;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IStrategyStatisService strategyStatisService;

    /**
     * 获取单个信息
     *
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public Object get(String id){

        return userInfoService.get(id);
    }



    // 验证手机号码是否存在
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean checkPhone = userInfoService.checkPhone(phone);
        return checkPhone;
    }


    /**
     * 发送和存验证码
     * @param phone
     * JsonResult在coreUtil里
     * @return
     */
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }


    /**
     *  注册
     * @param phone
     * @param nickname
     * @param password
     * @param rpassword
     * @param verifyCode
     * @return
     */
    @PostMapping("/regist")
    public Object regist(String phone, String nickname,String password,String rpassword,String verifyCode) {
            // 出现异常会被统一异常捕抓.
            userInfoService.regis(phone,nickname,password,rpassword,verifyCode);

            // 返回
            return  JsonResult.success();

    }


    /**
     * 登陆验证
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Object login(String username ,String password){
        // 登陆验证
        UserInfo user = userInfoService.login(username,password);

        // 存入token
       String token =  userInfoRedisService.setToken(user);

        // 设置一个Map
        // 返回回去
        HashMap map = new HashMap();
        map.put("token",token);
        map.put("user",user);
        return JsonResult.success(map);
    }

    /**
     * 获取当前登陆对象
     * @param request
     * @return
     */
    @GetMapping("/CurrentUser")
    @RequireLogin
    public  Object CurrentUser(HttpServletRequest request){
        // 拿到请求头 也就是token
        String token = request.getHeader("token");
        // 判断是否为空
        if (!StringUtils.hasLength(token)) {
            // 为空返回null
            return null;
        }
        // 拿到当前登陆对象
        UserInfo userInfo = userInfoRedisService.getToken(token);
        // 返回回去
        return JsonResult.success(userInfo);
    }


    /**
     * 拿到当前用户收藏的所有攻略id 的集合
     * @param sid
     * @param userInfo
     * @return
     */
    @GetMapping("/strategies/favor")
    public Object strategiesFavor(String sid, @UserParamLogin UserInfo userInfo){
        List<String> list = strategyStatisService.getStrategyStatisFavor(userInfo.getId());
        return JsonResult.success(list);

    }

}
